package com.zjj.algorithm.learning.atguigu.tree;

import javax.swing.plaf.PanelUI;

/**
 * @author zjj_admin
 * @date 2022/11/22 19:59
 */
public class TreeNode implements Comparable<TreeNode> {
    private int no;
    private TreeNode left;
    private TreeNode right;

    public TreeNode(int no) {
        this.no = no;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public TreeNode getLeft() {
        return left;
    }

    public void setLeft(TreeNode left) {
        this.left = left;
    }

    public TreeNode getRight() {
        return right;
    }

    public void setRight(TreeNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "TreeNode{" +
                "no=" + no +
                '}';
    }

    /**
     * 前序遍历方法
     */
    public void preOrder() {
        System.out.print(this + " -> ");
        //递归调用左子树
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
        }
    }

    /**
     * 中序遍历
     */
    public void midOrder() {
        //递归调用左子树
        if (this.left != null) {
            this.left.midOrder();
        }
        System.out.print(this + " -> ");
        //递归遍历右子树
        if (this.right != null) {
            this.right.midOrder();
        }
    }

    /**
     * 后续遍历
     */
    public void postOrder() {
        //递归调用左子树
        if (this.left != null) {
            this.left.postOrder();
        }
        //递归遍历右子树
        if (this.right != null) {
            this.right.postOrder();
        }
        System.out.print(this + " -> ");

    }


    /**
     * 前序遍历搜索
     * @param no
     * @return
     */
    public TreeNode preSearch(int no){
        System.out.println("inter preSearch...");
        //首先判断当前杰斯安是不是，是就直接返回
        if(this.no == no){
            return this;
        }
        TreeNode resNode = null;
        //需要判断当前节点的左子节点是否为 null ，当不为null时就递归继续前序查找
        if(this.left != null){
            resNode = this.left.preSearch(no);
        }
        //当左子节点找到了就返回即可
        if(resNode != null){
            return resNode;
        }
        if(this.right != null){
            resNode = this.right.preSearch(no);
        }
        return resNode;
    }


    /**
     * 中序遍历查找
     * @param no
     * @return
     */
    public TreeNode midSearch(int no){

        TreeNode resNode = null;
        if(this.left != null){
            resNode = this.left.midSearch(no);
        }
        if(resNode != null){
            return resNode;
        }
        System.out.println("inter midSearch...");
        if(this.no == no){
            return this;
        }
        if(this.right != null){
            resNode = this.right.midSearch(no);
        }
        return resNode;

    }

    public TreeNode postSearch(int no){

        TreeNode resNode = null;
        if(this.left != null){
            resNode = this.left.postSearch(no);
        }
        //在左子树找到
        if(resNode != null){
            return resNode;
        }
        //当左子树没有找到时，就想右子树遍历查找
        if(this.right != null){
            resNode = this.right.postSearch(no);
        }
        if(resNode != null){
            return resNode;
        }
        System.out.println("inter postSearch...");
        //当左右子树都没有找到时，就判断当前节点是不是即可

        if(this.no == no){
            return this;
        }
        return null;
    }


    /**
     * 删除节点，初级
     * 当删除的节点是叶子节点时，直接删除
     * 当删除节点存在子节点时，直接删除子节点子树
     * @param no
     */
    public void delNode(int no){

        if(this.left != null && this.left.no == no){
            this.left = null;
            return;
        }
        if(this.right != null && this.right.no == no){
            this.right = null;
            return;
        }

        if(this.left != null){
            this.left.delNode(no);
        }

        if(this.right != null){
            this.right.delNode(no);
        }
    }

    @Override
    public int compareTo(TreeNode o) {
        return this.no - o.no;
    }
}
